﻿<template>
  <van-popup v-model="visible" position="bottom" closeable>
    <van-form @submit="handleSubmit" :loading="loading">
      <van-field
        v-model="formFields.UserName"
        name="Name"
        label="Name"
        :rules="[{ required: true, message: '必填' }]"
      />
      ,
      <van-field
        v-model="formFields.UserName"
        name="Url"
        label="Url"
        :rules="[{ required: true, message: '必填' }]"
      />
      ,
      <van-field
        v-model="formFields.UserName"
        name="ThumbUrl"
        label="ThumbUrl"
        :rules="[{ required: true, message: '必填' }]"
      />
      ,
      <van-field
        v-model="formFields.UserName"
        name="Suffix"
        label="Suffix"
        :rules="[{ required: true, message: '必填' }]"
      />

      <div style="margin: 16px;">
        <van-button
          v-if="hasPerm('Base_UploadMap.Edit')"
          block
          type="info"
          native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </van-popup>
</template>

<script>
import {
  Base_UploadMap_GetTheData,
  Base_UploadMap_SaveData
} from "@/api/Base_Manage/Base_UploadMap";
import { mapGetters } from "vuex";

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      formFields: {
        Name: "",
        Url: "",
        ThumbUrl: "",
        Suffix: ""
      },
      entity: {}
    };
  },
  computed: {
    ...mapGetters(["hasPerm"])
  },
  methods: {
    add() {
      this.entity = {};
      this.visible = true;
      this.formFields = {
        Name: "",
        Url: "",
        ThumbUrl: "",
        Suffix: ""
      };
    },
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.formFields = {
          Name: "",
          Url: "",
          ThumbUrl: "",
          Suffix: ""
        };

        Base_UploadMap_GetTheData({ id: id }).then(resJson => {
          this.entity = resJson.Data;
          Object.keys(this.formFields).forEach(item => {
            this.formFields[item] = this.entity[item];
          });
        });
      });
    },

    handleSubmit(value) {
      this.entity = Object.assign(this.entity, this.formFields);
      this.loading = true;
      Base_UploadMap_SaveData(this.entity).then(resJson => {
        this.loading = false;

        if (resJson.Success) {
          this.$toast("操作成功");
          this.afterSubmit();
          this.visible = false;
        } else {
          this.$toast(resJson.Msg);
        }
      });
    }
  }
};
</script>
